package twitter.functionalities;

import java.sql.Connection;
import java.sql.Date;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import twitter.entity.Stat;
import twitter.entity.Tweet;
import twitter.tool.Database;
import twitter4j.IDs;
import twitter4j.Twitter;
import twitter4j.TwitterException;

public class StatComputer {

	private Database d;

	public StatComputer(Database d) throws TwitterException {
		this.d = d;
	}


	public Stat computeStat(int following, int follower) throws SQLException {
		List<Tweet> tweets = d.getAllTweet();
		Stat stat = new Stat();
		stat.setDate(new Date(Calendar.getInstance().getTime().getTime()));
		stat.setFollower(follower);
		stat.setFollowing(following);
		int nt = 0;
		float ar = 0;
		float af = 0;
		for (Tweet tweet : tweets) {
			ar = ar + tweet.getRetweet_count();
			af = af + tweet.getFavorite_count();
			nt++;
		}

		ar = ar / nt;
		af = af / nt;
		stat.setNt(nt);
		stat.setAf(af);
		stat.setAr(ar);
		return stat;

	}

	public void printDailyAvarageOfRT(List<Tweet> tweets) {
		Calendar start = Calendar.getInstance();
		Calendar finish = Calendar.getInstance();
		start.set(Calendar.MONTH, 5);
		start.set(Calendar.DAY_OF_MONTH, 7);
		finish.set(Calendar.MONTH, 8);
		finish.set(Calendar.DAY_OF_MONTH, 12);
		float nt = 0;
		float nRT = 0;
		do {
			float aRT = 0;
			for (Tweet tweet : tweets) {
				Calendar tweetDate = Calendar.getInstance();
				tweetDate.setTime(tweet.getDate());
				if (start.get(Calendar.YEAR) == tweetDate.get(Calendar.YEAR)
						&& start.get(Calendar.DAY_OF_YEAR) == tweetDate
								.get(Calendar.DAY_OF_YEAR)) {
					nt++;
					nRT = nRT + tweet.getRetweet_count();
				}
			}
			aRT = nRT / nt;
			System.out.println(aRT);
			start.set(Calendar.DAY_OF_YEAR, start.get(Calendar.DAY_OF_YEAR) + 1);
		} while (start.get(Calendar.YEAR) != finish.get(Calendar.YEAR)
				|| start.get(Calendar.DAY_OF_YEAR) != finish
						.get(Calendar.DAY_OF_YEAR));
	}

	public void printDailyAvarageOfP(List<Tweet> tweets) {
		Calendar start = Calendar.getInstance();
		Calendar finish = Calendar.getInstance();
		start.set(Calendar.MONTH, 5);
		start.set(Calendar.DAY_OF_MONTH, 7);
		finish.set(Calendar.MONTH, 8);
		finish.set(Calendar.DAY_OF_MONTH, 12);
		float nt = 0;
		float nP = 0;
		do {
			float aP = 0;
			for (Tweet tweet : tweets) {
				Calendar tweetDate = Calendar.getInstance();
				tweetDate.setTime(tweet.getDate());
				if (start.get(Calendar.YEAR) == tweetDate.get(Calendar.YEAR)
						&& start.get(Calendar.DAY_OF_YEAR) == tweetDate
								.get(Calendar.DAY_OF_YEAR)) {
					nt++;
					nP = nP + tweet.getFavorite_count();
				}
			}
			aP = nP / nt;
			System.out.println(aP);
			start.set(Calendar.DAY_OF_YEAR, start.get(Calendar.DAY_OF_YEAR) + 1);
		} while (start.get(Calendar.YEAR) != finish.get(Calendar.YEAR)
				|| start.get(Calendar.DAY_OF_YEAR) != finish
						.get(Calendar.DAY_OF_YEAR));
	}

	public void printDailyNumberOfT(List<Tweet> tweets) {
		Calendar start = Calendar.getInstance();
		Calendar finish = Calendar.getInstance();
		start.set(Calendar.MONTH, 5);
		start.set(Calendar.DAY_OF_MONTH, 7);
		finish.set(Calendar.MONTH, 8);
		finish.set(Calendar.DAY_OF_MONTH, 12);
		int nt = 0;
		do {
			for (Tweet tweet : tweets) {
				Calendar tweetDate = Calendar.getInstance();
				tweetDate.setTime(tweet.getDate());
				if (start.get(Calendar.YEAR) == tweetDate.get(Calendar.YEAR)
						&& start.get(Calendar.DAY_OF_YEAR) == tweetDate
								.get(Calendar.DAY_OF_YEAR)) {
					nt++;
				}
			}
			System.out.println(nt);
			start.set(Calendar.DAY_OF_YEAR, start.get(Calendar.DAY_OF_YEAR) + 1);
		} while (start.get(Calendar.YEAR) != finish.get(Calendar.YEAR)
				|| start.get(Calendar.DAY_OF_YEAR) != finish
						.get(Calendar.DAY_OF_YEAR));
	}

	public void printDailyNumberOfRT(List<Tweet> tweets, Connection conn)
			throws SQLException {
		Calendar start = Calendar.getInstance();
		Calendar finish = Calendar.getInstance();
		start.set(Calendar.MONTH, 5);
		start.set(Calendar.DAY_OF_MONTH, 7);
		finish.set(Calendar.MONTH, 8);
		finish.set(Calendar.DAY_OF_MONTH, 12);
		int nRT;
		do {
			nRT = 0;
			for (Tweet tweet : tweets) {
				Calendar tweetDate = Calendar.getInstance();
				tweetDate.setTime(tweet.getDate());
				if (start.get(Calendar.YEAR) == tweetDate.get(Calendar.YEAR)
						&& start.get(Calendar.DAY_OF_YEAR) == tweetDate
								.get(Calendar.DAY_OF_YEAR)) {
					nRT = nRT + tweet.getRetweet_count();
				}
			}
			System.out.println(nRT);
			start.set(Calendar.DAY_OF_YEAR, start.get(Calendar.DAY_OF_YEAR) + 1);
		} while (start.get(Calendar.YEAR) != finish.get(Calendar.YEAR)
				|| start.get(Calendar.DAY_OF_YEAR) != finish
						.get(Calendar.DAY_OF_YEAR));
		Database d = new Database(conn);
		List<Stat> list = d.getAllStat();

		for (Stat s : list) {
			nRT = 0;
			start.setTime(s.getDate());
			for (Tweet tweet : tweets) {
				Calendar tweetDate = Calendar.getInstance();
				tweetDate.setTime(tweet.getDate());
				if (start.get(Calendar.YEAR) == tweetDate.get(Calendar.YEAR)
						&& start.get(Calendar.DAY_OF_YEAR) == tweetDate
								.get(Calendar.DAY_OF_YEAR)) {
					nRT = nRT + tweet.getRetweet_count();
				}
			}
			System.out.println(nRT);
		}
	}

	public void printDailyNumberOfP(List<Tweet> tweets, Connection conn)
			throws SQLException {
		Calendar start = Calendar.getInstance();
		Calendar finish = Calendar.getInstance();
		start.set(Calendar.MONTH, 5);
		start.set(Calendar.DAY_OF_MONTH, 7);
		finish.set(Calendar.MONTH, 8);
		finish.set(Calendar.DAY_OF_MONTH, 12);
		int nP;
		do {
			nP = 0;
			for (Tweet tweet : tweets) {
				Calendar tweetDate = Calendar.getInstance();
				tweetDate.setTime(tweet.getDate());
				if (start.get(Calendar.YEAR) == tweetDate.get(Calendar.YEAR)
						&& start.get(Calendar.DAY_OF_YEAR) == tweetDate
								.get(Calendar.DAY_OF_YEAR)) {
					nP = nP + tweet.getFavorite_count();
				}
			}
			System.out.println(nP);
			start.set(Calendar.DAY_OF_YEAR, start.get(Calendar.DAY_OF_YEAR) + 1);
		} while (start.get(Calendar.YEAR) != finish.get(Calendar.YEAR)
				|| start.get(Calendar.DAY_OF_YEAR) != finish
						.get(Calendar.DAY_OF_YEAR));
		Database d = new Database(conn);
		List<Stat> list = d.getAllStat();

		for (Stat s : list) {
			nP = 0;
			start.setTime(s.getDate());
			for (Tweet tweet : tweets) {
				Calendar tweetDate = Calendar.getInstance();
				tweetDate.setTime(tweet.getDate());
				if (start.get(Calendar.YEAR) == tweetDate.get(Calendar.YEAR)
						&& start.get(Calendar.DAY_OF_YEAR) == tweetDate
								.get(Calendar.DAY_OF_YEAR)) {
					nP = nP + tweet.getFavorite_count();
				}
			}
			System.out.println(nP);
		}
	}

	

	
}
